<?php
namespace Liaiyong\Light\Action;

use Liaiyong\Light\Base\RESTable;
use Liaiyong\Light\Core\Action;
use Liaiyong\Light\Core\Error;
use Liaiyong\Light\Core\Request;
use Liaiyong\Light\Data\DataResponse;
use Liaiyong\Light\Util\Utility;

abstract class Cgi extends Action implements RESTable {
    /**
     * 事件常量，GET时
     *
     * @var string
     */
    const E_GET = 'cgi:event:get';
    /**
     * 事件常量，POST时
     *
     * @var string
     */
    const E_POST = 'cgi:event:post';
    /**
     * 事件常量，PUT
     *
     * @var string
     */
    const E_PUT = 'cgi:event:put';
    /**
     * 事件常量，DELETE
     *
     * @var string
     */
    const E_DELETE = 'cgi:event:delete';
    /**
     * 事件常量，HEAD
     *
     * @var string
     */
    const E_HEAD = 'cgi:event:head';
    /**
     * 事件常量，PATCH
     *
     * @var string
     */
    const E_PATCH = 'cgi:event:patch';
    /**
     * 事件常量，OPTIONS
     *
     * @var string
     */
    const E_OPTIONS = 'cgi:event:options';

    /**
     * UserAgent
     * @var string
     */
    protected $ua;
    /**
     * Request
     * @var Request
     */
    protected $request;
    /**
     * 是否有上传文件部分
     */
    protected $hasMultipart = false;
    /**
     * 参数
     * @var DataResponse
     */
    protected $response;
    /**
     * Action初始化
     *
     * @return void
     */
    public function initialize() {
        parent::initialize();
        $this->ua = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
        $this->request = Request::getInstance();
        $this->response = new DataResponse();
        // listen
        $this->event->listen(get_class($this), Action::E_LANUCH, array($this, 'afterLanuch'));
        // init some variables
        $this->hasMultipart = $this->request->isMultipart();
    }
    /**
     * @return Override
     */
    public function lifecycle() {
        try {
            parent::lifecycle();
        } catch(Error $e) {
            $this->failure($e);
            $this->template->display();// display
        }
    }
    /**
     * onLanuch方法执行后
     *
     * @return void
     */
    public function afterLanuch() {
        $method = $this->request->getMethod();
        $method = empty($method) ? 'get' : $method;
        // on
        $this->{'on' . ucfirst(strtolower($method))}();
        $this->event->fire($class, $this->method2Event($method), array (
                $this 
        ));
    }
    /**
     * 将HTTP方法转换为事件常量
     *
     * @return void
     */
    private function method2Event($method) {
        switch (strtoupper($method)) {
            case 'POST' :
                $event = Cgi::E_POST;
                break;
            case 'PUT' :
                $event = Cgi::E_PUT;
                break;
            case 'DELETE' :
                $event = Cgi::E_DELETE;
                break;
            case 'PATCH' :
                $event = Cgi::E_PATCH;
                break;
            case 'HEAD' :
                $event = Cgi::E_HEAD;
                break;
            case 'OPTIONS' :
                $event = Cgi::E_OPTIONS;
                break;
            case 'GET' :
            default :
                $event = Cgi::E_GET;
                break;
        }
        return $event;
    }

    /**
     * 失败的Response
     */
    protected function failure($code, $msg = null) {
        $this->response = empty($this->response) ? new DataResponse() : $this->response;
        $this->response->rsp = 0;
        $this->response->api = $this->app->getApiname();
        $this->response->data = $msg;
        $this->response->code = $code;
        $this->template->push($this->response);
        $this->app->logAccess(Error::__lastError());
        $this->app->logError(Error::__lastError());
    }
    /**
     * 成功的Response
     */
    protected function success($data) {
        $this->response = empty($this->response) ? new DataResponse() : $this->response;
        $this->response->rsp = 1;
        $this->response->api = $this->app->getApiname();
        $this->response->code = 0;
        $this->response->data = $data;
        $this->template->push($this->response);
    }

    /**
     * GET事件
     */
    public function onGet() {
    }
    /**
     * POST事件
     */
    public function onPost() {
    }
    /**
     * PUT事件
     */
    public function onPut() {
    }
    /**
     * DELETE事件
     */
    public function onDelete() {
    }
    /**
     * HEAD事件
     */
    public function onHead() {
    }
    /**
     * PATCH事件
     */
    public function onPatch() {
    }
    /**
     * OPTIONS事件
     */
    public function onOptions() {
    }
    /**
     * 渲染输出内容
     */
    public function onRender() {
        @header('X-Powered-By: liaiyong-light');
        $rep = $this->request->getExtension();
        switch ($rep) {
            case 'json' :
                $this->template->json();
                break;
            case 'xml' :
                $this->template->xml();
                break;
            case 'css' :
                $this->template->cssp();
                break;
            case 'csv' :
                $this->template->csv();
                break;
            /*case 'src' :
                $pathname = Autoloader::getClass(get_class($this));
                highlight_file($pathname);
                break;*/
            case 'png' :
            case 'jpg' :
            case 'jpeg' :
            case 'gif' :
                $this->template->image($rep);
                break;
            case '' :
            case 'html' :
            case 'htm' :
            case 'php' :
                $this->template->display();
                break;
            case 'js' :
            case 'jsonp' :
            default:
                $this->template->jsonp();
                break;
        }
    }
}